from django.db.models import Q
from rest_framework import viewsets
from ERP_system.utils.multiple_destory import MultipleDestoryMixin,MultipleOpenMixin
from ERP_system.utils.pagination import GlobalPagination

from basic_info.models import WarehouseModel
from basic_info.serializer.warehouse_serializer import WarehouseSerializer,WarehouseSearchSerializer

class WarehouseView(viewsets.ModelViewSet,MultipleDestoryMixin,MultipleOpenMixin):

    queryset = WarehouseModel.objects.all()

    serializer_class = WarehouseSerializer


    pagination_class = GlobalPagination

    def get_serializer_class(self):
        if self.action=='list':
           return  WarehouseSearchSerializer

        return WarehouseSerializer
#搜索框过滤查询
    def get_queryset(self):

        if self.action=='list':
            #接受三个可能的参数
            name=self.request.data.get('name',None)
            remark = self.request.data.get('remark', None)


            #因为参数可能没有，造成过滤条件是动态的

            query=Q()

            if name:
                query.add(Q(name__contains=name),'AND')
            if remark:
                query.add(Q(remark__contains=remark),'AND')

            return WarehouseModel.objects.filter(query).all()

        else:

            return WarehouseModel.objects.all()











